/*
 * estadoInicial.java
 *
 * Created on 5 de septiembre de 2008, 09:26 AM
 */

package administrador;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;

public class balanceGeneral extends javax.swing.JPanel {
    Conexion conexion;
    String fecha="";
    int filasA=0;
    int filasP=0;
    int filasPT=0;
    
    List vMayor;
    //Nath Redondeo
    NumberFormat nf = NumberFormat.getNumberInstance();
    
    public balanceGeneral(Conexion conexion,List vMayor) {
        DecimalFormat dec=(DecimalFormat)nf;
        dec.applyPattern("###.##");
         //Configuro Redondeo
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.HALF_UP);
        
        this.vMayor=vMayor;
        this.conexion=conexion;
        initComponents();
        llenoDatos();
        
        
    }
    
    public void llenoDatos(){
        String consulta="";
        //Activos
        consulta="select * from tercernivel  where cod_primernivel=1 order by cod_primernivel";
        java.sql.ResultSet consultaActivo=conexion.consultarBase(consulta);
        muestroTablaA(consultaActivo,consulta,tabla_activo,true);
        filasA=conexion.obtenerFilas(consultaActivo);    
        
        //Pasivos:
        consulta="select * from tercernivel  where cod_primernivel=2 order by cod_primernivel";
        consultaActivo=conexion.consultarBase(consulta);
        muestroTablaA(consultaActivo,consulta,tabla_pasivo,true);
        filasP=conexion.obtenerFilas(consultaActivo);    
        
        //Patrimonios:
        consulta="select * from tercernivel  where cod_primernivel=3 order by cod_primernivel";
        consultaActivo=conexion.consultarBase(consulta);
        muestroTablaA(consultaActivo,consulta,tabla_pa,true);
        filasPT=conexion.obtenerFilas(consultaActivo);    
        fecha=new java.sql.Date(System.currentTimeMillis()).toString();
    
        //Lleno Gastos:
        consulta="select * from tercernivel  where cod_primernivel=4 order by cod_primernivel";
        consultaActivo=conexion.consultarBase(consulta);
        muestroTablaA(consultaActivo,consulta,tabla_activo,false);
        filasPT=conexion.obtenerFilas(consultaActivo);    
        fecha=new java.sql.Date(System.currentTimeMillis()).toString();
    
        
        
        //Lleno Ingresos
        consulta="select * from tercernivel  where cod_primernivel=5 order by cod_primernivel";
        consultaActivo=conexion.consultarBase(consulta);
        muestroTablaA(consultaActivo,consulta,tabla_pa,false);
        filasPT=conexion.obtenerFilas(consultaActivo);    
        fecha=new java.sql.Date(System.currentTimeMillis()).toString();
    
        actualizoTotales();
        
        
    }
    
    public int cuentaFilaOcupadas(JTable tabla){
        int totalfilas=tabla.getRowCount();
        int contador=0;
        Object valor;
        for (contador=0;contador<totalfilas;contador++) {
            valor=tabla.getValueAt(contador,0);
            if (valor==null){
                break;
            }
            
        }
        //System.out.println("El numero de fila ocupadas es:"+contador);
        return contador;
    }
    
    public void asientoActivos(){
      try{
        conexion.consultarBase("START TRANSACTION;");
        String numeroDiario=numeroDiario();
        asiento(numeroDiario,fecha,"Asiento Incial de Activos");
        String numeroAsiento=numeroAsiento();
        for(int i=0;i<filasA;i++){
          try{
                  String valor=tabla_activo.getValueAt(i,4).toString();
                  if(valor.equals("")){
                      valor="0";    
                  }
                  asientoDeatlle(numeroAsiento,numeroDiario,tabla_activo.getValueAt(i,0).toString(),tabla_activo.getValueAt(i,1).toString(),tabla_activo.getValueAt(i,2).toString(),"0",valor,"0");
          }catch(java.lang.NullPointerException f){}
          
        }
        //asiento(numeroDiario,fecha,"Asiento Incial de Pasivos");
        //String numeroAsiento1=numeroAsiento();
        for(int i=0;i<filasP;i++){
            try{
                  String valor=tabla_pasivo.getValueAt(i,4).toString();
                  if(valor.equals("")){
                      valor="0";    
                  }
                  asientoDeatlle(numeroAsiento,numeroDiario,tabla_pasivo.getValueAt(i,0).toString(),tabla_pasivo.getValueAt(i,1).toString(),tabla_pasivo.getValueAt(i,2).toString(),"0","0",valor);
            }catch(java.lang.NullPointerException f){}
        }
        
        //asiento(numeroDiario,fecha,"Asiento Incial de Patrimonio");
        // numeroAsiento2=numeroAsiento();
        for(int i=0;i<filasPT;i++){
            try{
                      String valor=tabla_pa.getValueAt(i,4).toString();
                      if(valor.equals("")){
                          valor="0";    
                      }
                      asientoDeatlle(numeroAsiento,numeroDiario,tabla_pa.getValueAt(i,0).toString(),tabla_pa.getValueAt(i,1).toString(),tabla_pa.getValueAt(i,2).toString(),"0","0",valor);
              }catch(java.lang.NullPointerException f){}
        }
        conexion.insertarDatos("insert into estadoinicial(diario) values("+numeroDiario+")");
        conexion.consultarBase("COMMIT;");
        conexion.limpiar(tabla_pa);
        conexion.limpiar(tabla_pasivo);
        conexion.limpiar(tabla_activo);
        txt_totalActivo.setText("");
        txt_totalPasivo.setText("");
        txt_TotalPatrimonio.setText("");
        txt_cuadre.setText("");
     }catch(Exception sqle){
        conexion.consultarBase("ROLLBACK;");
     }
     
    }
    public boolean verificaExistencia(){
        boolean existe=false;
        String numeroDiario=numeroDiario();
            try{   
                if(numeroDiario.equals(null)){}else{
                        existe=false;
                    if(incialDiario().equals(null)){
                        existe=false;
                    }else{
                        if(numeroDiario.equals(incialDiario())){
                            existe=true;
                        }
                    }
                }
            }catch(java.lang.NullPointerException h){} 
        return existe;
    }
    public String incialDiario(){
        String numero="";
        try{
            java.sql.ResultSet r=conexion.consultarBase("select MAX(diario) from estadoinicial");
            r.next();
            numero=r.getString(1);
        }
        catch(java.lang.NullPointerException h){} 
        catch(java.sql.SQLException f){
        }
        return numero;
    }
    public String numeroDiario(){
         String numero=""; 
         try{
            java.sql.ResultSet r=conexion.consultarBase("select  cod_diario from diario where fecha_diario=(select max(fecha_diario) from diario) limit 1");
            r.next();
            numero=r.getString(1);
          }catch(java.sql.SQLException f){}
         return numero;
    }
    public String numeroAsiento(){
         String numero=""; 
         try{
            java.sql.ResultSet r=conexion.consultarBase("select max(cod_asiento) from asiento");
            r.next();
            numero=r.getString(1);
          }catch(java.sql.SQLException f){}
         return numero;
    }
    public void asiento(String numeroDiario,String fecha,String referencia){
        conexion.insertarDatos("insert into asiento(cod_diario,fecha_dia,referencia) values("+numeroDiario+",'"+fecha+"','"+referencia+"')");
    }
    public void asientoDeatlle(String numeroAsiento,String numeroDiario,String n3,String n2,String n1,String parcial,String debe,String haber){
        String consultaDtlAs= "insert into dtl_asiento "+
                              "(cod_diario,cod_asiento,cod_tercernivel,cod_segundonivel,cod_primernivel,parcial,debe,haber) " +
                              "values ("+numeroDiario+","+numeroAsiento+","+n3+","+n2+","+n1+","+parcial+","+debe+","+haber+")";
        conexion.insertarDatos(consultaDtlAs);
    }
    
    public void muestroTablaA(java.sql.ResultSet respuesta,String consulta,javax.swing.JTable tabla,boolean limpiar){
        //Limpio la tabla 
        if(limpiar){
        limpiarTabla(tabla);
        }
        
        java.util.Vector vecPrincipal=conexion.consulta(consulta);
        java.util.Vector vecInterno=new java.util.Vector();
        for(int i=0;i<vecPrincipal.size();i++){
           
            //Saco la ultima fila desocupada:
            int fila=cuentaFilaOcupadas(tabla);
            
           vecInterno=(java.util.Vector)vecPrincipal.elementAt(i);
           tabla.setValueAt(vecInterno.elementAt(2),fila,0);  
           tabla.setValueAt(vecInterno.elementAt(1),fila,1); 
           tabla.setValueAt(vecInterno.elementAt(0),fila,2); 
           tabla.setValueAt(vecInterno.elementAt(3),fila,3); 
           
           //Saco y Pongo Valor en la tabla:
           double valor=sacoValor(vecInterno.elementAt(0),vecInterno.elementAt(1),vecInterno.elementAt(2));
           
          //System.out.println("El pongo el valor"+valor+"- en la fila:"+fila+"-en la columna 4");
           tabla.setValueAt(valor,fila,4); 
           
          
        }
    } 
      
    public double sacoValor(Object c3,Object c2,Object c1){
    double valor=0;
    List datos=new ArrayList();
    
    //Saco Codigo de la Cuenta:
    String codigo=c1.toString()+","+c2.toString()+","+c3.toString();
   //System.out.println("El codigo de la Cuentas es # "+codigo);
    
    int contador=vMayor.size();
    for(int i=0;i<contador;i++){
    datos=(List) vMayor.get(i);
    String codigoM=datos.get(1).toString();
   //System.out.println("Compara:"+codigo+"=="+codigoM);
    if(codigo.equals(codigoM)){
    valor=Double.parseDouble(datos.get(2).toString());
   //System.out.println("El valor es:"+valor);
    break;
    }
    }        
    
    return valor;
    }
    
    
    public void limpiarTabla(JTable tabla){
    //Limpio Tabla
        int filas=tabla.getRowCount();
        int cont=0;
        while(cont<filas){
            try{
                for(int i=0;i<6;i++){
                    //System.out.println("valor fila:"+cont+" columnas: "+i);
                    tabla.setValueAt(null,cont,i);
                    ////System.out.println(toma);
                }
            }catch(Exception e){};
            cont++;
        }
    
    }
    
    public double sumaValores(javax.swing.JTable tabla){
        int fila=cuentaFilaOcupadas(tabla)-1;
        double suma=0.0;
        for(int i=0;i<fila;i++){
          try{
                  String valor=tabla.getValueAt(i,4).toString();
                  if(valor.equals("")){
                      valor="0.0";    
                  }
                  suma+=Double.parseDouble(valor);
          }catch(java.lang.NullPointerException f){}
        }
        return suma;
    }
    
    public void actualizoTotales(){
    //Actualizo Activos:    
    txt_totalActivo.setText(nf.format(sumaValores(tabla_activo)).replaceAll(",","."));
    txt_totalPasivo.setText(nf.format(sumaValores(tabla_pasivo)).replaceAll(",","."));
    txt_TotalPatrimonio.setText(nf.format(sumaValores(tabla_pa)).replaceAll(",","."));
    //double totPasPatr=Double.parseDouble(txt_totalPasivo.getText())+Double.parseDouble(txt_TotalPatrimonio.getText());
    //double sumaTotal=totPasPatr;
    //txt_cuadre.setText(String.valueOf(totPasPatr));
    }
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txt_totalActivo = new javax.swing.JTextField();
        txt_totalPasivo = new javax.swing.JTextField();
        txt_TotalPatrimonio = new javax.swing.JTextField();
        pn_activos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla_activo = new javax.swing.JTable();
        pn_pasivo = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabla_pasivo = new javax.swing.JTable();
        pn_patrimonio = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabla_pa = new javax.swing.JTable();
        txt_cuadre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        setLayout(null);

        txt_totalActivo.setEditable(false);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(balanceGeneral.class);
        txt_totalActivo.setText(resourceMap.getString("txt_totalActivo.text")); // NOI18N
        txt_totalActivo.setName("txt_totalActivo"); // NOI18N
        add(txt_totalActivo);
        txt_totalActivo.setBounds(240, 540, 60, 20);

        txt_totalPasivo.setEditable(false);
        txt_totalPasivo.setText(resourceMap.getString("txt_totalPasivo.text")); // NOI18N
        txt_totalPasivo.setName("txt_totalPasivo"); // NOI18N
        add(txt_totalPasivo);
        txt_totalPasivo.setBounds(580, 260, 60, 20);

        txt_TotalPatrimonio.setEditable(false);
        txt_TotalPatrimonio.setText(resourceMap.getString("txt_TotalPatrimonio.text")); // NOI18N
        txt_TotalPatrimonio.setName("txt_TotalPatrimonio"); // NOI18N
        add(txt_TotalPatrimonio);
        txt_TotalPatrimonio.setBounds(580, 520, 60, 20);

        pn_activos.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("pn_activos.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null)); // NOI18N
        pn_activos.setName("pn_activos"); // NOI18N
        pn_activos.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla_activo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nivel 1", "Nivel 2", "Nivel 3", "Cuenta", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla_activo.setName("tabla_activo"); // NOI18N
        tabla_activo.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                tabla_activoInputMethodTextChanged(evt);
            }
        });
        tabla_activo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tabla_activoPropertyChange(evt);
            }
        });
        tabla_activo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tabla_activoKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(tabla_activo);

        pn_activos.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 20, 312, 471));

        add(pn_activos);
        pn_activos.setBounds(10, 40, 318, 491);

        pn_pasivo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pasivos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null));
        pn_pasivo.setName("pn_pasivo"); // NOI18N
        pn_pasivo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tabla_pasivo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nivel 1", "Nivel 2", "Nivel 3", "Cuenta", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla_pasivo.setName("tabla_pasivo"); // NOI18N
        tabla_pasivo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tabla_pasivoPropertyChange(evt);
            }
        });
        jScrollPane2.setViewportView(tabla_pasivo);

        pn_pasivo.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 20, 316, 194));

        add(pn_pasivo);
        pn_pasivo.setBounds(340, 40, 322, 214);

        pn_patrimonio.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("pn_patrimonio.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null)); // NOI18N
        pn_patrimonio.setName("pn_patrimonio"); // NOI18N
        pn_patrimonio.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        tabla_pa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nivel 1", "Nivel 2", "Nivel 3", "Cuenta", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla_pa.setName("tabla_pa"); // NOI18N
        tabla_pa.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tabla_paPropertyChange(evt);
            }
        });
        jScrollPane3.setViewportView(tabla_pa);

        pn_patrimonio.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 20, 318, 202));

        add(pn_patrimonio);
        pn_patrimonio.setBounds(340, 290, 330, 222);

        txt_cuadre.setEditable(false);
        txt_cuadre.setText(resourceMap.getString("txt_cuadre.text")); // NOI18N
        txt_cuadre.setName("txt_cuadre"); // NOI18N
        add(txt_cuadre);
        txt_cuadre.setBounds(580, 550, 60, 20);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1);
        jLabel1.setBounds(512, 260, 70, 20);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        add(jLabel2);
        jLabel2.setBounds(440, 520, 140, 20);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        add(jLabel3);
        jLabel3.setBounds(440, 550, 141, 20);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        add(jLabel4);
        jLabel4.setBounds(121, 540, 120, 20);

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setForeground(resourceMap.getColor("jLabel5.foreground")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        add(jLabel5);
        jLabel5.setBounds(10, 10, 209, 22);
    }// </editor-fold>//GEN-END:initComponents
int numero;
private void tabla_activoPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tabla_activoPropertyChange
 try{
  txt_totalActivo.setText(nf.format(sumaValores(tabla_activo)).replaceAll(",","."));
  double totPasPatr=Double.parseDouble(txt_totalPasivo.getText())+Double.parseDouble(txt_TotalPatrimonio.getText());
  double sumaTotal=totPasPatr;
  txt_cuadre.setText(nf.format(totPasPatr).replaceAll(",","."));
 }catch(Exception e){}
}//GEN-LAST:event_tabla_activoPropertyChange

private void tabla_activoInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_tabla_activoInputMethodTextChanged
      
}//GEN-LAST:event_tabla_activoInputMethodTextChanged

private void tabla_pasivoPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tabla_pasivoPropertyChange
    txt_totalPasivo.setText(nf.format(sumaValores(tabla_pasivo)).replaceAll(",","."));
    double totPasPatr=Double.parseDouble(txt_totalPasivo.getText())+Double.parseDouble(txt_TotalPatrimonio.getText());
    double sumaTotal=totPasPatr;
    txt_cuadre.setText(nf.format(totPasPatr).replaceAll(",","."));
    
}//GEN-LAST:event_tabla_pasivoPropertyChange

private void tabla_paPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tabla_paPropertyChange
    txt_TotalPatrimonio.setText(nf.format(sumaValores(tabla_pa)).replaceAll(",","."));
    double totPasPatr=Double.parseDouble(txt_totalPasivo.getText())+Double.parseDouble(txt_TotalPatrimonio.getText());
    double sumaTotal=totPasPatr;
    txt_cuadre.setText(nf.format(totPasPatr).replaceAll(",","."));
}//GEN-LAST:event_tabla_paPropertyChange

private void tabla_activoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabla_activoKeyTyped
}//GEN-LAST:event_tabla_activoKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel pn_activos;
    private javax.swing.JPanel pn_pasivo;
    private javax.swing.JPanel pn_patrimonio;
    private javax.swing.JTable tabla_activo;
    private javax.swing.JTable tabla_pa;
    private javax.swing.JTable tabla_pasivo;
    private javax.swing.JTextField txt_TotalPatrimonio;
    private javax.swing.JTextField txt_cuadre;
    public javax.swing.JTextField txt_totalActivo;
    private javax.swing.JTextField txt_totalPasivo;
    // End of variables declaration//GEN-END:variables

}
